// pages/feedBack/feedBack.js
const app = getApp() //获取app实列
const ajax = require("../../utils/ajax.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    cases:[],
    imgUrl:[]
  },
  submitClick:function(){
    return wx.showToast({
      title: '此功能暂未开放',
      icon:"none"
    })
  },
  clearClick:function(e){
    let index = e.currentTarget.dataset.index;
    let arrImg = this.data.imgUrl;
    let arrCases = this.data.cases;
    arrImg.splice(index, 1)
    arrCases.splice(index, 1)
    this.setData({
      imgUrl: arrImg,
      cases: arrCases
    })
  },
  uploadImg:function(){
    let that = this;
    //上传图片
    let imgArr = this.data.cases;
    let imgUrlArr = this.data.imgUrl;
    wx.chooseImage({
      count: 5,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {

        let token = wx.getStorageSync("token")
        wx.showLoading({
          title: '拼命上传中..',
          icon: "none"
        })
        let pathArr = res.tempFilePaths
        for (var i = 0; i < pathArr.length; i++) {
          wx.uploadFile({
            url: app.url.upload, //仅为示例，非真实的接口地址
            filePath: res.tempFilePaths[i],
            name: 'file',
            formData: {
              token: token
            },
            success: function (img) {
              let data = JSON.parse(img.data)
              if (data.code !== 200) {
                return wx.showToast({
                  title: data.msg,
                  icon: "none"
                })
              }
              imgArr.push(JSON.parse(img.data).data.path)
              imgUrlArr.push(JSON.parse(img.data).data.url)
              wx.hideLoading()
              that.setData({
                cases: imgArr
              })
              that.setData({
                imgUrl: imgUrlArr
              })

            }
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})